<?php

namespace app\commons\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\RouteNotFoundException;
use think\exception\ValidateException;
class Http extends Handle
{
    public function render(Exception $e)
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {
            return json(['message'=>$e->getError(),'code'=>422,'data'=>[]], 422);
        }
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }

        if($e instanceof RouteNotFoundException){
            return response($e->getMessage(), 403);
        }

        if($e instanceof \Firebase\JWT\SignatureInvalidException){
            return json(['message'=>$e->getMessage(),'code'=>401,'data'=>['login_url'=>douYinUrl()]], 401);
        }
        if($e instanceof \Firebase\JWT\BeforeValidException){
            return json(['message'=>$e->getMessage(),'code'=>401,'data'=>['login_url'=>douYinUrl()]], 401);
        }
        if($e instanceof \Firebase\JWT\ExpiredException){
            return json(['message'=>$e->getMessage(),'code'=>401,'data'=>['login_url'=>douYinUrl()]], 401);
        }
        // 其他错误交给系统处理
        return parent::render($e);
    }
}